/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ig.metrica.ui.editor.da;

import ig.ejb.metrica.da.session.editor.EditorSBean;
import ig.ejb.metrica.da.session.editor.dto.ColumnaDTO;
import ig.ejb.metrica.da.session.editor.dto.TablaDTO;
import ig.metrica.origenesdedatos.*;
import java.rmi.RemoteException;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 *
 * @author jsanchez
 */
public class MEditorMetricaDA {


    public List<TablaDTO> getAllTables() throws Exception, RemoteException {
       return lookupModeloSBean().getAllTables();
    }

    public List<ColumnaDTO> getColumnTables(String nameTable) throws Exception, RemoteException {
        return lookupModeloSBean().getColumnTables(nameTable);
    }

    public Map<String,Object> getData(TablaDTO dTO) throws Exception, RemoteException {
        return lookupModeloSBean().getData(dTO);
    }

    public List<Map> getData(String nameTable, Timestamp fechaCorte) throws Exception, RemoteException {
        return lookupModeloSBean().getData(nameTable, fechaCorte);
    }

    public List<Map> getDataODato(int idODato,String lLabel, String lValue, int more20) throws Exception, RemoteException {
        return lookupModeloSBean().getDataODato(idODato, lLabel,  lValue, more20);
    }

    public void update(TablaDTO tablaDTO) throws Exception, RemoteException {
        lookupModeloSBean().update(tablaDTO);
    }

    public int updateRecord(String tabla, String columna, int id, Object data) throws Exception, RemoteException {
        return lookupModeloSBean().updateRecord(tabla, columna, id, data);
    }

    public int createRecord(String tabla, Map data) throws Exception, RemoteException {
        return lookupModeloSBean().createRecord(tabla, data);
    }

    public int deleteRecord(int id, String tabla) throws Exception, RemoteException {
        return lookupModeloSBean().deleteRecord(id, tabla);
    }


    public void delete(TablaDTO tablaDTO) throws Exception, RemoteException {
        lookupModeloSBean().delete(tablaDTO);
    }

    public Collection getOrigenesDatos()throws Exception, RemoteException {
        return lookupOrigenesDatos().findByAllDTO();
    }

    public OrigenesdatosDTO getOriegenDeDato(Integer id)throws Exception, RemoteException {
        Collection<OrigenesdatosDTO> collection = getOrigenesDatos();
        for (OrigenesdatosDTO dTO : collection) {
            if(dTO.getConsecutivo().equals(id))
                return dTO;
        }
        return null;
    }

    private static EditorSBean lookupModeloSBean() {
        /*try {
            Properties prop = new Properties();
            prop.put(Context.INITIAL_CONTEXT_FACTORY, IGBean.INITIAL_CONTEXT_FACTORY);
            String Sockec = new ConexionServer().getSocketServer();
            prop.put(Context.PROVIDER_URL, Sockec);

            javax.naming.Context c = new javax.naming.InitialContext(prop);
            Object remote = c.lookup("EditorSBean");
            EditorRemoteHome rv = (EditorRemoteHome) javax.rmi.PortableRemoteObject.narrow(remote, EditorRemoteHome.class);
            return rv.create();
        } catch (javax.naming.NamingException ne) {
            throw new RuntimeException(ne);
        } catch (javax.ejb.CreateException ce) {
            throw new RuntimeException(ce);
        } catch (java.rmi.RemoteException re) {
            throw new RuntimeException(re);
        }
        */
        return new EditorSBean();
    }

    private static OrigenDeDatosBean lookupOrigenesDatos() {
        /*try {
            Properties prop = new Properties();
            prop.put( Context.INITIAL_CONTEXT_FACTORY, IGBean.INITIAL_CONTEXT_FACTORY);
            String Sockec = new ConexionServer().getSocketServer();
            prop.put( Context.PROVIDER_URL, Sockec);
            InitialContext ctx = new InitialContext(prop);
            OrigenDeDatosHome home = (OrigenDeDatosHome)ctx.lookup("ig.metrica.origenesdedatos.OrigenDeDatos");            
            return home.findByPrimaryKey(new OrigenDeDatosPK(1));
        } catch (FinderException ex) {
            throw new RuntimeException(ex);
        } catch (javax.naming.NamingException ne) {
            throw new RuntimeException(ne);
        } catch (java.rmi.RemoteException re) {
            throw new RuntimeException(re);
        }*/
        return new OrigenDeDatosBean();
    }


}
